// pages/exam3/exam3.js

let app = getApp()
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        index:{
          type:Number,
          value:0,
          observer:'watchIndex'
        },
        detail: {
            type: Object,
            value: {},
            observer: 'watch1'
        },
        items: {
            type: Object,
            value: {}
        },
        submit: {//是否提交
            type: Boolean,
            value: false,
            observer: 'watch'
        },
        my_answer: {//我选择的答案
            type: Object,
            value: []
        },
        num: {
            type: Number,
            value: 0
        },
        type: {//1为章节列表进的2就是考试//3為错题本进来的//4是直接重笔记本里和考同学进来的
            type: Number,
            value: 0
        },
        tabid: {
            type: Number,
            value: 0
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        tabid: 0,
        fen: '',//分数
        fen2: {},
        my_answer_c: '',//答案输入
        subjectInfo: {},
        ti: false,//是否展开题
        domain: app.domain
    },

    lifetimes: {
      show: function() {
        // 页面被展示
        console.log("sssssssss");
      },
      ready:function() {
        // 页面被展示
        console.log("rrrrrrrrrrrrrrr");
      },
      attached: function() {
        // 在组件实例进入页面节点树时执行
        console.log("wewewewewe");
      },
      detached: function() {
        // 在组件实例被从页面节点树移除时执行
        console.log("abababababaabab");
      },
    },

    /**
     * 组件的方法列表
     */
    methods: {
        watchIndex(n,o){
            this.setData({
                tabid:n
            })
        },
        watch1(newV, oV) {
          console.log(newV,'date677776736476346367364');
          this.setData({  //初始获取数据，直接标记为1
            tabid:0
          })
            this.get_mk_subject_info(newV.subject_id,newV.report_id);
        },
        //获取题目详情
        get_mk_subject_info(subject_id,report_id) {
            wx.$request(wx.$api.mk_subject_info, {
                subject_id,
                report_id
            }).then(res => {
                res.mk_ask.forEach(item => item.flage = false)

                

                console.log(res);
                this.setData({
                    subjectInfo: res,
                    fen: res.mk_ask[this.data.index].mk_answer_ask_log?.fraction || '',
                    my_answer_c:res.mk_ask[this.data.index].mk_answer_ask_log?.answer_content || ''
                })
            })
        },
        qie(e) {
            let {index} = e.currentTarget.dataset
            let fen = ''
            if (this.data.fen2[index]) {
                fen = this.data.fen2[index]
            }
            this.setData({
                tabid: index,
                fen: this.data.subjectInfo.mk_ask[index].mk_answer_ask_log?.fraction || '',
                my_answer_c:this.data.subjectInfo.mk_ask[index].mk_answer_ask_log?.answer_content || ''

            })
            var pageArr = getCurrentPages();
            var thisPage = pageArr[pageArr.length - 1];
            thisPage.setData({
                tabid: index
            })
        },
        bindchinge(e) {
            this.setData({
                fen: e.detail.value
            })
        },
        bindtis() {
            this.setData({
                ti: !this.data.ti
            })
        },
        check(e) {//选择答案
            let {index} = e.currentTarget.dataset
            let {tabid} = this.data
            let detail = this.data.detail
            if (this.data.type == 2) {//考试，需要单独处理
                this.check2(index, tabid)
                return
            }
            for (let i = 0; i < subjectInfo.mk_ask[tabid].option_str.length; i++) {
                subjectInfo.mk_ask[tabid].option_str[i].flage = false
            }
            subjectInfo.mk_ask[tabid].option_str[index].flage = true
            console.log(detail, 'exam3')
            this.setData({
                detail
            })
        },
        check2(index, tabid) {//考试的时候选择答案
            let detail = this.data.detail
            for (let i = 0; i < subjectInfo.mk_ask[tabid].option_str.length; i++) {
                subjectInfo.mk_ask[tabid].option_str[i].flage = false
            }
            subjectInfo.mk_ask[tabid].option_str[index].flage = true
            this.setData({
                detail
            })
            let my_answer = []//我的答案//和这道题的分数和这道题的正确答案
            for (let i = 0; i < subjectInfo.mk_ask.length; i++) {
                my_answer = my_answer.concat({
                    answer: '',
                    fraction: subjectInfo.mk_ask[i].fraction,
                    correct_answer: subjectInfo.mk_ask[i].correct_answer
                })
                for (let j = 0; j < subjectInfo.mk_ask[i].option_str.length; j++) {
                    if (subjectInfo.mk_ask[i].option_str[j].flage) {
                        my_answer[i].answer = subjectInfo.mk_ask[i].option_str[j].option
                    }
                }
            }
            var pageArr = getCurrentPages();
            var thisPage = pageArr[pageArr.length - 1];
            thisPage.setData({
                [`items2[${this.data.num}].subject_log.my_answer`]: my_answer,
                [`items2[${this.data.num}].is_correct`]: 1 //记录这道题做了
            })
        },
        bindmy_answer(e) {
            console.log(e)
            this.setData({
                my_answer_c: e.detail.value
            })
        },
        flage() {//答案的展开与收齐
            let detail = this.data.detail
            // console.log(this.data)
            if (!this.data.my_answer_c) {
                wx.showToast({
                    title: '请先作答后再对照答案打分',
                    icon: "none"
                })
                return false;
            }
            this.setData({
                [`subjectInfo.mk_ask[${this.data.tabid}].flage`]: !this.data.subjectInfo.mk_ask[this.data.tabid].flage
            })
        },
        watch(newDate, oldDate) {//监听是否已经提交答案了,然后赋值对错
            this.setData({
                option_str: []
            })
            console.log(newDate, oldDate, 'exam3')
            if (newDate) {
                if (this.data.detail.type == 3) {
                    let my_answer = this.data.my_answer//正确答案
                    let little_question_json = this.data.subjectInfo.mk_ask
                    console.log(little_question_json)
                    for (let i = 0; i < little_question_json.length; i++) {
                        for (let j = 0; j < little_question_json[i].option_str.length; j++) {
                            little_question_json[i].option_str[j].dui = false//复制成统一的false
                            little_question_json[i].option_str[j].cuo = false//复制成统一的false
                            if (little_question_json[i].option_str[j].is_correct == 1) {
                                little_question_json[i].option_str[j].dui = true
                            } else {
                                if (my_answer.length != 0) {
                                    if (little_question_json[i].option_str[j].is_correct == 2 && my_answer[i].answer == little_question_json[i].option_str[j].option) {
                                        little_question_json[i].option_str[j].cuo = true
                                    }
                                }
                            }
                        }
                    }
                    this.setData({
                        option_str: little_question_json
                    })
                } else {
                    let my_answer = this.data.my_answer//正确答案
                    this.setData({
                        fen: my_answer[0],
                        fen2: my_answer
                    })
                }
            }
        },
        sure() {//填分数的确认
          var pageArr = getCurrentPages();
          var thisPage = pageArr[pageArr.length - 1];
          
            console.log(thisPage.data.questionList);
            console.log(thisPage.data.subjectInfo);
            if (!this.data.my_answer_c) {
                wx.showToast({
                    title: '请输入答案',
                    icon: 'none'
                })
                return;
            }
            if (!this.data.subjectInfo.mk_ask[this.data.tabid].flage){
                wx.showToast({
                    title: '请先查看参考答案',
                    icon: 'none'
                })
                return;
            }
            if (this.data.fen.length == 0) {
                wx.showToast({
                    title: '请输入分数',
                    icon: 'none'
                })
                return
            }
            if (this.data.fen > Number(this.data.subjectInfo.mk_ask[this.data.tabid].fraction)) {
                wx.showToast({
                    title: '自评分不能超过题目参考分',
                    icon: 'none'
                })
                return
            }
            this.setData({
                [`fen2.${this.data.tabid}`]: this.data.fen
            })
            if (this.data.type == 2) {
                var pageArr = getCurrentPages();
                var thisPage = pageArr[pageArr.length - 1];
                let fraction = []//记录，每道题的分数
                for (let i = 0; i < this.data.subjectInfo.mk_ask.length; i++) {
                    fraction = fraction.concat({fraction: this.data.subjectInfo.mk_ask[i].fraction})
                }
                thisPage.setData({
                    [`items2[${this.data.num}].fen2`]: this.data.fen2,
                    [`items2[${this.data.num}].fraction`]: fraction,
                    [`items2[${this.data.num}].is_correct`]: 1 //记录这道题做了
                })
                this.mk_submit_subject_big(this.data.subjectInfo.mk_ask[this.data.tabid].id,this.data.fen,this.data.my_answer_c,this.data.detail.report_id)
            }

            // console.log(this.data.fen2, 'xxx', this.data.fen2[0], 'x', this.data.fen2[1])
        },
        mk_submit_subject_big(ask_id, fraction, answer_content,report_id) {
          
          
          
          

            wx.$request(wx.$api.mk_submit_subject_big,{
                ask_id,
                fraction,
                answer_content,
                report_id
            }).then(res=>{
              let subjectInfoss=this.data.subjectInfo;
              let ask_log={
                 fraction:fraction,
                  answer_content:answer_content
              };
              subjectInfoss.mk_ask[this.data.tabid].mk_answer_ask_log=ask_log;
             
              this.setData({
                subjectInfo:subjectInfoss,//存贮答案 和分数
              });
              
              console.log(this.data.subjectInfo,"8889898989ddddddd");

                if (this.data.subjectInfo.mk_ask.length > this.data.tabid+1){
                    this.setData({
                        
                        tabid:this.data.tabid+=1,
                        fen: this.data.subjectInfo.mk_ask[this.data.tabid].mk_answer_ask_log?.fraction || '',
                        my_answer_c:this.data.subjectInfo.mk_ask[this.data.tabid].mk_answer_ask_log?.answer_content || ''
                    })
                }
            })
            .catch(err=>{

                console.log("ccccccccccccccccccccc");

                if (this.data.subjectInfo.mk_ask.length > this.data.tabid+1){
                    this.setData({
                        tabid:this.data.tabid+=1,
                        fen: this.data.subjectInfo.mk_ask[this.data.tabid].mk_answer_ask_log?.fraction || '',
                        my_answer_c:this.data.subjectInfo.mk_ask[this.data.tabid].mk_answer_ask_log?.answer_content || ''
                    })
                }
            }) 
        },
        bindti() {//提交
            var pageArr = getCurrentPages();
            var thisPage = pageArr[pageArr.length - 1];
            let answer = []
            for (let i = 0; i < this.data.subjectInfo.mk_ask.length; i++) {
                if (this.data.fen2[i]) {
                    answer = answer.concat(1)
                }
            }
            if (answer.length != this.data.subjectInfo.mk_ask.length) {
                wx.showToast({
                    title: '全部确定才能提交哟',
                    icon: 'none'
                })
                return
            }
            thisPage.submit()
        },
    }
})

